#!/bin/bash -e
# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
source /usr/libexec/fb-common-functions

config_fan_max31790()
{
    # Set fan tack sample count for each tach channel to highest value to avoid
    # wrong fan tach reading.
    #
    # FanX_Dynamics
    # - register offset: 0x08 (tach1) - 0x0d (tach6)
    # |------------------------ -----------------------------------------------|
    # | bit[7:5]         | 000b | 001b | 010b | 011b | 110b | 101b, 110b, 111b |
    # |------------------------------------------------------------------------|
    # | tach sample count|  1   |  2   |  4   |  8   |  16  |  32              |
    # |------------------------------------------------------------------------|
    for addr in "0x21" "0x27"
    do
        for offset in $(seq 0x08 0x0d)
        do
            fan_dynam_val=$(printf "%d" "$(i2cget -y -f 18 "$addr" "$offset")")
            fan_dynam_val=$((fan_dynam_val | 0xe0))
            i2cset -y -f 18 "$addr" "$offset" "$fan_dynam_val"
        done

        # after manual set FanX_Dynamics, needs to rebind driver to ensure
        # driver get correct setitngs
        bus_addr="$(printf "18-%04x" $addr)"
        echo "$bus_addr" > /sys/bus/i2c/drivers/max31790/unbind
        echo "$bus_addr" > /sys/bus/i2c/drivers/max31790/bind
    done

    # set fan enable
    if find /sys/bus/i2c/devices/*/hwmon/*/fan*_enable -print -quit 2>/dev/null | grep -q .; then
        for file in /sys/bus/i2c/devices/*/hwmon/*/fan*_enable
        do
            echo 1 > "${file}"
        done
    fi
}

config_fan()
{
    if [ -d "/sys/bus/i2c/drivers/max31790/18-0021/hwmon" ]; then
        echo "Configure MAX31790 fan controller"
        config_fan_max31790
    fi
}

config_ltc4287_hsc()
{
    echo "Configure LTC4287 HSC"
    # Set ADC samples per average to 4096 in MFR_AVG_SEL (D9h) reg
    i2cset -y -f 20 0x42 0xd9 0x8b || echo "Failed to set MFR_AVG_SEL to HSC1 (20-0042)"
    i2cset -y -f 20 0x43 0xd9 0x8b || echo "Failed to set MFR_AVG_SEL to HSC2 (20-0043)"
}

config_xdp710_hsc()
{
    echo "Configure XDP710 HSC"
    # Set telemetry averaging samples to 128 in TELEMETRY_AVG (E9h) reg
    i2cset -y -f 20 0x13 0xe9 0x01ff w || echo "Failed to set TELEMETRY_AVG to HSC1 (20-0013)"
    i2cset -y -f 20 0x1c 0xe9 0x01ff w || echo "Failed to set TELEMETRY_AVG to HSC2 (20-001c)"
}

config_hsc()
{
    if [ "$(i2cdetect -y -q 20 0x13 0x13 | grep "10:" | awk '{print $2}')" != "--" ]; then
        config_xdp710_hsc
    else
        config_ltc4287_hsc
    fi
}

# Enable standby power and bind i2c components
/usr/libexec/catalina/standby-power-enable

# Workaround
# rebind PDB i2c mux
bind_i2c_device pca954x 1-0070
# rebind PDB Fan IC
bind_i2c_device max31790 18-0021
bind_i2c_device max31790 18-0027
# rebind PDB FRU
bind_i2c_device at24 21-0054
# rebind FIO FRU
bind_i2c_device at24 23-0053

# Set fan target and enable all fan input as early as possible
config_fan

# configure hsc
config_hsc

exit 0
